﻿using System.IO;
using System.Text;

namespace SubRepair.ChangeEncoding
{
    public class ConvertToUTF8
    {
        protected Encoding SourceEncoding { get; set; }
        protected string FileAddress { get; set; }
        protected string FileDirectory { get; set; }
        protected string FileName { get; set; }
        protected string FileExt { get; set; }

        protected string SourceString { get; set; }
        protected string FinalString { get; set; }

        public ConvertToUTF8(int codepage)
        {
            this.SourceEncoding = Encoding.GetEncoding(codepage);
            
        }

        public void Set(string fileAddress)
        {
            this.FileAddress = fileAddress;
            this.FileDirectory = Path.GetDirectoryName(fileAddress) + @"\";
            this.FileName = Path.GetFileNameWithoutExtension(fileAddress);
            this.FileExt = Path.GetExtension(fileAddress).ToLower();
        }

        public virtual void Convert()
        { }

        protected void Save()
        {
            string newName = this.FileName + " -Repaired" + this.FileExt;

            StreamWriter writer = new StreamWriter(FileDirectory + newName);
            writer.Write(this.FinalString);
            writer.Close();
        }
    }

}